package qldc.pm.tienan.searchcontainer;

import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;

import qldc.pm.tienan.searchcontainer.util.congdan.CongDanComparatorUtil;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class CongDanSearch extends SearchContainer<CongDanSearch> {
	public static final String EMPTY_RESULTS_MESSAGE = "khong-tim-thay-cong-dan-nao";

	public CongDanSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
		super(portletRequest, new CongDanDisplayTerms(portletRequest),
				new CongDanSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
				DEFAULT_DELTA, iteratorURL, null, EMPTY_RESULTS_MESSAGE);

		CongDanDisplayTerms displayTerms = (CongDanDisplayTerms) getDisplayTerms();

		iteratorURL.setParameter(CongDanSearchTerms.SO_DINH_DANH_CA_NHAN , displayTerms.getSoDinhDanhCaNhan());
		iteratorURL.setParameter(CongDanSearchTerms.SO_CMND , displayTerms.getSoCMND());
		iteratorURL.setParameter(CongDanSearchTerms.SO_SO_HO_KHAU, displayTerms.getSoSoHoKhau());
		iteratorURL.setParameter(CongDanSearchTerms.HO_VA_TEN , displayTerms.getHoVaTen());
		iteratorURL.setParameter(CongDanSearchTerms.NOI_THUONG_TRU_TINH_ID , String.valueOf(displayTerms.getNoiThuongTruTinhId()));
		iteratorURL.setParameter(CongDanSearchTerms.NOI_THUONG_TRU_QUAN_HUYEN_ID , String.valueOf(displayTerms.getNoiThuongTruQuanHuyenId()));
		iteratorURL.setParameter(CongDanSearchTerms.NOI_THUONG_TRU_PHUONG_XA_ID , String.valueOf(displayTerms.getNoiThuongTruPhuongXaId()));
		iteratorURL.setParameter(CongDanSearchTerms.NOI_THUONG_TRU_DIA_CHI , displayTerms.getNoiThuongTruDiaChi());

		try {
			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(portletRequest);

			String orderByCol = ParamUtil.getString(portletRequest, "orderByCol");
			String orderByType = ParamUtil.getString(portletRequest,"orderByType");
			String portletName = "tienan";

			if(Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)){
				preferences.setValue(portletName, "order-by-col", orderByCol);
				preferences.setValue(portletName, "order-by-type", orderByType);

			} else{
				orderByCol = preferences.getValue(portletName, "order-by-col", "");
				orderByType = preferences.getValue(portletName, "order-by-type", "asc");
			}

			OrderByComparator orderByComparator = CongDanComparatorUtil.getCongDanOrderByComparator(orderByCol, orderByType);

			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		} catch (SystemException e) {
 		}
	}
}
